<?php

/**
 * Basic PHP template. Uses PHP (no special template syntax) in the template files,
 * but keeps global variables out and only allows variables passed in to be available.
 * 
 * PHP versions 4 and 5
 * 
 * Licensed under The MIT License
 * 
 * @author Jacob Wright
 * @copyright		Copyright (c) 2006, Within Code LLC
 * @package			blueprint
 * @version			$Revision: $
 * @modifiedby		$LastChangedBy: $
 * @lastmodified	$Date: 2006-05-25 16:12:33 -0600 (Thu, 25 May 2006) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */



class PHPTemplate {
	
	/**
	 * Holds all the variables for the template
	 * @var array
	 */
	var $vars = array();
	
	/**
	 * The path to the template file
	 * @var string
	 */
	var $file;
	
	/**
	 * Filters which will be run on the template after it is processed
	 * @var array
	 */
	var $filters = array();
	
	
	
	/**
	 * Constructor to instantiate the template
	 * 
	 * @param string $file Path to the template file which will be used
	 */
	function PHPTemplate($file) {
		// Get the filename and the cache directory
		$this->file = $file;
	}
	
	/**
	 * Adds a filter to be run on the template after it is processed
	 *
	 * @param string/array $function Function name or an array(obj $object, string $method)
	 */
	function addFilter($function) {
		$this->filters[] = $function;
	}
	
	/**
	 * Assign data to a variable name for the template.
	 * 
	 * @param string $name Name of the variable as it will be known inside the template
	 * @param mixed $value Data (string, int, object, array) that will be assigned to specified variable name
	 */
	function assign($name, $value) {
		$this->vars[$name] = $value;
	}
	
	/**
	 * Unassigns an assigned variable name
	 * 
	 * @param string $name The name of the assigned variable being unassigned
	 */
	function unassign($name) {
		unset($this->vars[$name]);
	}
	
	/**
	 * Returns the value of an assigned variable in the template
	 * 
	 * @param string $name The variable name
	 * @return mixed The value of the variable being asked for
	 */
	function getVar($name) {
		return $this->vars[$name];
	}
	
	/**
	 * Assigns all the values in an associative array to corresponding variables in the template
	 * 
	 * @param array $vars An associative array with the name value pairs of the variables to be added
	 * @param boolean $clear An optional parameter which, if set to true, will clear all previously assigned variables
	 */
	function addVars($vars, $clear = false) {
		if($clear) $this->vars = $vars or $this->vars = array();
		elseif(is_array($vars)) $this->vars = array_merge($this->vars, $vars);
	}
	
	/**
	 * Returns an associative array of the variables assigned to this template
	 * 
	 * @return array An associative array of all variables in template
	 */
	function getVars() {
		return $this->vars;
	}
	
	/**
	 * Processes the template and returns it's output
	 * 
	 * @return string The final output of the template
	 */
	function fetch() {
		ob_start();
		extract($this->vars);
		include($this->file);
		$contents = ob_get_contents();
		ob_end_clean();
		foreach ($this->filters as $filter) {
			if (is_array($filter) && method_exists($filter[0], $filter[1]))
				$contents = $filter[0]->$filter[1]($contents);
			elseif (function_exists($filter))
				$contents = $filter($contents);
		}
		return $contents;
	}
	
	/**
	 * Processes the template and prints it's output to the screen
	 */
	function display() {
		echo $this->fetch();
	}
}

?>
